package com.dragon.mylibrary.manager;

import android.bluetooth.BluetoothDevice;
import android.text.TextUtils;


import com.dragon.blelibrary.scandevise.BleDeviceScan;
import com.dragon.blelibrary.scandevise.IScanListener;
import com.dragon.utilslibrary.AppConfig;

import java.util.List;

/**
 * 搜索
 */
public class BleScanUtil {
    private static BleScanUtil instance;
    private BluetoothDevice scanDevice ;
    private BleScanUtil(){}
    public static synchronized BleScanUtil getInstance(){
        if(instance == null)
            instance = new BleScanUtil();
        return instance;
    }

    /**
     * 根据地址搜索设备，
     * @param list 升级和正常的设备地址
     * @param time 搜索时间
     * @param callback 如果搜索到，就有值，null表示没有搜索到
     */
    public void scanByAddress(final List<String> list, long time, final ScanUtilCallback callback){
        if(list == null || list.isEmpty())
            return;
        scanDevice = null;
        final BleDeviceScan mDeviceScan = BleDeviceScan.getInstance(AppConfig.mContext);
        mDeviceScan.stopScanDevice();
        mDeviceScan.scanDevice(time, new IScanListener() {
            @Override
            public void OnScanning() {

            }

            @Override
            public void OnScanEnd(int result) {
                if(result == IScanListener.RESULT_SCAN_NO_DEVICE || result == IScanListener.RESULT_SCAN_TIME_OUT){
                    callback.onScanCallback(scanDevice);
                }
            }

            @Override
            public void OnScanNewDevice(BluetoothDevice device, String name, int rssi, byte[] scanRecord) {
                if(device != null && list.contains(device.getAddress())){
                    scanDevice = device;
                    mDeviceScan.stopScanDevice();
                    callback.onScanCallback(scanDevice);
                }
            }
        });
    }

    public void scanByAddress(final String mac, long time, final ScanUtilCallback callback){
        if(TextUtils.isEmpty(mac))
            return;
        scanDevice = null;
        final BleDeviceScan mDeviceScan = BleDeviceScan.getInstance(AppConfig.mContext);
        mDeviceScan.stopScanDevice();
        mDeviceScan.scanDevice(time, new IScanListener() {
            @Override
            public void OnScanning() {

            }

            @Override
            public void OnScanEnd(int result) {
                if(result == IScanListener.RESULT_SCAN_NO_DEVICE || result == IScanListener.RESULT_SCAN_TIME_OUT){
                    if (callback != null){
                        callback.onScanCallback(scanDevice);
                    }
                }
            }

            @Override
            public void OnScanNewDevice(BluetoothDevice device, String name, int rssi, byte[] scanRecord) {
                if(device != null && rssi != -1 && mac.equalsIgnoreCase(device.getAddress())){
                    scanDevice = device;
                    mDeviceScan.stopScanDevice();
                    if (callback != null){
                        callback.onScanCallback(scanDevice);
                    }
                }
            }
        });
    }


    public interface ScanUtilCallback{
        /**
         * 搜索结果回调
         * @param device
         */
        void onScanCallback(BluetoothDevice device);
    }
}
